package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.queryBean.WaterTypeQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.baseConfig.vo.WaterTypeVO;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.newProjectCenter.vo.PjMeterExportVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工程报装-工程登记节点水表明细数据导出
 */
public class PjMeterRegisterExport extends AbstractExportExcel {
    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
        PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
        UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
        MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
        WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);

        List<PjMeter> pjMeterList = new ArrayList<>();
        if (StringUtils.isNotEmpty(pjMeterQuery.getConsultId())){
            // 根据工程主键ID查询出水表信息
            pjMeterList.addAll(pjMeterMapper.getList(pjMeterQuery));
        }
        List<PjMeterExportVO> pjMeterExportVOList = new ArrayList<>();
        if (pjMeterList != null && pjMeterList.size() > 0) {
            // 将水表明细中用户ID转成list
            List<String> userInfoTempIdList = pjMeterList.stream().map(PjMeter::getUserInfoTempId).collect(Collectors.toList());
            List<UserBean> userBeanList = userInfoMapper.selectListByIds(userInfoTempIdList, UserBean.USERTEMP);
            Map<String, UserBean> userGroup = userBeanList.stream().collect(Collectors.toMap(UserBean::getId, Function.identity()));

            // 水表口径列表
            List<MeterBoreVO> meterBoreList = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
            Map<String, String> meterBoreMap = meterBoreList.stream().collect(Collectors.toMap(MeterBoreVO::getId, MeterBoreVO::getBoreName));

            // 用水类型
            List<WaterTypeVO> waterTypeList = waterTypeMapper.findAllWaterType(new WaterTypeQueryBean());
            Map<String, WaterTypeVO> waterTypeGroup =waterTypeList.stream().collect(Collectors.toMap(WaterTypeVO::getId, Function.identity()));
            pjMeterList.forEach(pjMeter -> {
                PjMeterExportVO pjMeterExportVO = new PjMeterExportVO();
                UserBean userBean = userGroup.get(pjMeter.getUserInfoTempId());
                // 水表口径
                if (StringUtils.isNotEmpty(pjMeter.getMeterBore())){
                    pjMeterExportVO.setMeterBore(pjMeter.getMeterBore());
                    pjMeterExportVO.setMeterBoreName(meterBoreMap.get(pjMeter.getMeterBore()));
                }else {
                    pjMeterExportVO.setMeterBore("");
                    pjMeterExportVO.setMeterBoreName("");
                }

                // 用水类型
                if (StringUtils.isNotEmpty(userBean.getUseWaterType())){
                    WaterTypeVO waterType = waterTypeGroup.get(userBean.getUseWaterType());
                    pjMeterExportVO.setUserWaterType(waterType.getId());
                    pjMeterExportVO.setUserWaterTypeName(waterType.getWaterTypeName());
                }else {
                    pjMeterExportVO.setUserWaterType("");
                    pjMeterExportVO.setUserWaterTypeName("");
                }

                // 设用户编号
                pjMeterExportVO.setUserNo(userBean.getUserNo());
                // 设置装表地址
                pjMeterExportVO.setSetupMeterAddr(userBean.getSetupMeterAddr());

                pjMeterExportVOList.add(pjMeterExportVO);
            });
        }

        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("PjMeterRegisterExport.btl");
        attachment.setFileTemplate("PjMeterRegisterExport.xlsx");
        attachment.setData(pjMeterExportVOList);
        attachment.setFilename("工程报装-工程登记导出模板");
        attachment.setFileType("excel");
        return attachment;
    }
}
